package com.wzgl.core.webmagic.utils;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.wzgl.core.model.Tgrid;
import com.wzgl.core.webmagic.MagicUtils;

public class PicUtiles {
	
	/**
	 * 下载图片
	 * @param pic_urls 图片链表
	 * @param t 保存对象
	 * @return 返回更新的对象
	 */
	public static Tgrid downPics(List<String> pic_urls,Tgrid t){
		String src = "";
		 String src2 = "";
		 String c_url = "";
		 String c_url2 = "";
		 //下载一张作为封面
		 if(pic_urls.size()<2){
			 Document docList = Jsoup.parse(pic_urls.get(0));
			 if(docList != null){
				 Elements liTag=docList.getElementsByTag("img");
				 if(!liTag.get(0).attr("src").contains("http")){
					 String http_src = "http:"+liTag.get(0).attr("src");
					 src = http_src;
				 }else{
					 src = liTag.get(0).attr("src");
				 }
   			     c_url = MagicUtils.downloadPic(src);
			 }
		 }else{
			 Document docList = Jsoup.parse(pic_urls.get(0));
			 Document docList2 = Jsoup.parse(pic_urls.get(1));
			 if(docList != null){
				 Elements liTag=docList.getElementsByTag("img");
				 if(!liTag.get(0).attr("src").contains("http")){
					 String http_src = "http:"+liTag.get(0).attr("src");
					 src = http_src;
				 }else{
					 src = liTag.get(0).attr("src");
				 }
   			     c_url = MagicUtils.downloadPic(src);
			 }
			 if(docList2 != null){
				 Elements liTag=docList2.getElementsByTag("img");
				 if(!liTag.get(0).attr("src").contains("http")){
					 String http_src = "http:"+liTag.get(0).attr("src");
					 src2 = http_src;
				 }else{
					 src2 = liTag.get(0).attr("src");
				 }
   			     c_url2 = MagicUtils.downloadPic(src2);
			 }
		 }
    	 t.setFcoverImg(c_url);
    	 t.setFcoverImg2(c_url2);
		 System.out.println("img-1:"+c_url);
    	 System.out.println("img-2:"+c_url2);
    	 return t;
	}
	
	/**
	 * 单张图片下载
	 * @param url 原有链接
	 * @return 返回  下载好的图片链接
	 */
	public static String downPic(String url){
		String src = "";
		String newhref = "";
		Document doc = Jsoup.parse(url);
		if(doc != null){
			 Elements liTag = doc.getElementsByTag("img");
			 if(!liTag.get(0).attr("src").contains("http")){
				 String http_src = "http:"+liTag.get(0).attr("src");
				 src = http_src;
			 }else{
				 src = liTag.get(0).attr("src");
			 }
			 newhref = MagicUtils.downloadPic(src);
		}
		return newhref;
	}
	
	/**
	 * 替换原有正文的图片list中所有的图片
	 * @param pic_urls 图片list
	 * @param cont 原有正文
	 * @param pic_hrefs 
	 * @return 新的正文
	 */
	public static String replaceAllPic(List<String> pic_urls, String cont) {
		String newcont = "";
		String temp = cont;
		for(String img : pic_urls){
			String oldimgsrc = "";
			Document doc = Jsoup.parse(img);
			if(doc != null){
				 Elements liTag=doc.getElementsByTag("img");
				 oldimgsrc = liTag.get(0).attr("src");
			}
			String newimgsrc =  downPic(img);
			//替换
			temp = temp.replace(oldimgsrc, newimgsrc);
		}
		newcont = temp;
		return newcont;
	}

	/**
	 * 替换原有正文中所有的图片链接和图片
	 * @param pic_hrefs  原有链接
	 * @param pic_urls2     原有图片地址
	 * @param cont 原有正文
	 * @return 返回新的正文
	 */
	public static String replaceAllHrefAndImg(List<String> pic_hrefs, List<String> pic_urls, String cont) {
		String newcont = "";
		String temp = cont;
		List<String> imgsrclist = new ArrayList<String>(); 
		for(String img : pic_urls){
			String oldimgsrc = "";
			String http_imgsrc = "";
			Document doc = Jsoup.parse(img);
			if(doc != null){
				 Elements liTag=doc.getElementsByTag("img");
				 oldimgsrc = liTag.get(0).attr("src");
				 if(!liTag.get(0).attr("src").contains("http")){
					 String http_src = "http:"+liTag.get(0).attr("src");
					 http_imgsrc = http_src;
				 }else{
					 http_imgsrc = liTag.get(0).attr("src");
				 }
			}
			//替换	
			String newimgsrc =  downPic(img);
			temp = temp.replace(oldimgsrc, newimgsrc);
			imgsrclist.add(newimgsrc);
		}
		if(pic_hrefs.size()>0 && pic_hrefs != null){
			for(int i=0;i<pic_hrefs.size();i++){
				String oldhref = "";
				Document doc = Jsoup.parse(pic_hrefs.get(i));
				if(doc != null){
					 Elements liTag=doc.getElementsByTag("a");
					 oldhref = liTag.get(0).attr("href");
					//替换链接
					if(!oldhref.equals("javascript:") && !oldhref.equals("") && oldhref != null){
						temp = temp.replace(oldhref, "javascript:");
					}
				}	
			}
		}
		newcont = temp;
		return newcont;
	}
	
	/**
	 * 替换所有链接  和  图片src
	 * @param cont 内容
	 * @return  返回更新的内容
	 */
	public static String replaceAllHrefAndImg(String cont) {
		String newcont = cont;
		Document docs = Jsoup.parse(cont);
		//图片
		Elements ds = docs.getElementsByTag("img");
		String oldimgsrc = "";
		if(ds != null && ds.size() > 0){
			for(Element s:ds){
				 oldimgsrc = s.attr("src") == null || "".equalsIgnoreCase(s.attr("src"))?s.attr("data-src"):s.attr("src");
				 if(oldimgsrc != null && !"".equals(oldimgsrc)){
					 String newimgsrc =  downPic(s.toString());
					 newcont = newcont.replace(oldimgsrc, newimgsrc);
				 }
			}
		}
		//链接
		Elements a_ds = docs.getElementsByTag("a");
		String olda = "";
		if(a_ds != null && a_ds.size() > 0){
			for(Element s:a_ds){
				olda = s.attr("href");
				 if(olda != null && !"".equals(olda)){
					 String newa =  downPic(s.toString());
					 newcont = newcont.replace(olda, newa);
				 }
			}
		}
//		for(String img : pic_urls){
//			String oldimgsrc = "";
//			Document doc = Jsoup.parse(img);
//			if(doc != null){
//				 Elements liTag=doc.getElementsByTag("img");
//				 oldimgsrc = liTag.get(0).attr("src") == null || "".equalsIgnoreCase(liTag.get(0).attr("src"))?liTag.get(0).attr("data-src"):liTag.get(0).attr("src");
//			}
//			//替换
//			String newimgsrc =  downPic(img);
//			temp = temp.replace(oldimgsrc, newimgsrc);
//			imgsrclist.add(newimgsrc);
//		}
		return newcont;
	}
}
